package com.mqtt.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

import com.mqtt.MqttApplication;
import com.mqtt.mqtt.Mqtt;

public class MqttConnectService extends Service {

    private Mqtt mMqtt;


    public MqttConnectService() {

    }

    /**
     * 该类用于在onBind方法执行后返回的对象，
     * 该对象对外提供了该服务里的方法
     */
    private class MyBinder extends Binder implements IMqttConnectBinder {

        @Override
        public void onConnect() {
            try {
                mMqtt = new Mqtt(getApplicationContext(), MqttApplication.mMqttConnectionBean);
                Thread.sleep(3*100);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onPublish(String msg) {
            try {
                mMqtt.publishMessage(msg);
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "连接服务器后再发送", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onDisConnect() {
            try {
                mMqtt.disConnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public boolean isConnect() {
            return false;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }



    @Override
    public void onCreate() {
        super.onCreate();

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
